const userMod = require('../model/userMod.js')
const { enCode } = require('../utils/enCode.js')
const JWT = require('../utils/jwt.js')

// 该层处理数据库参数
const userServ = {
  wxloginServ: async (data) => {
    const uID = await enCode(data.code) // 解析code为uid
    const res = await userMod.isRegister(uID)
    console.log(res);
    if (res === 1) {
      // 已注册
      const res = await userMod.selectwxPerson(uID) // 查询
      return res[0]
    } else {
      // 未注册
      return {
        isRegister: 0,
        uID: uID
      }
    }
  },
  webloginServ: async (data) => {
    const code1 = await userMod.validateAccMOd(data)
    if (code1 === 0) return {
      code: 1001,
      msg: '账号不存在！',
      data:null
    }
    const code2 = await userMod.validatePWdMOd(data)
    if (code2 === 0) return {
      code: 1001,
      msg: '密码错误！',
      data:null
    }
    const res = await userMod.selectWebuser(data)
    return {
      code: 0,
      msg: '登录成功！',
      token: JWT.generate({
        acc: data.acc,
        pwd: data.pwd
      }, '1d'),
      data:res[0]
    }
  },
  wxRegisterServ: async (data) => {
    await userMod.createwxUser(data) // 创建用户
    const res = await userMod.selectwxPerson(data.uID) // 查询
    return res[0]
  },
  editAnnouceServ: async (data) => {
    await userMod.editAnnouceMod(data)
  },
  webAppeallistServ: async (data) => {
    const res = await userMod.webAppeallistMod(data)
    return res
  },
  userUpAppealServ: async (data) => {
    await userMod.createAppeal(data)
  },
  userEditInfoServ: async (data) => {
    await userMod.userEditInfoMod(data)
  },
  userEvoServ: async (data) => {
    await userMod.userEvoMod(data)
  },
  userAppealServ: async (data) => {
    const res = await userMod.userAppealMod(data)
    return res
  },
  userlistServ: async (data) => {
    const res = await userMod.userlistMod(data)
    return res
  },
  userHealServ: async (data) => {
    const res = await userMod.userHealMod(data)
    return res
  },
  deleteAppealwebServ: async (data) => {
    await userMod.deleteAppealwebMod(data)
  },
  editAppealServ: async (data) => {
    await userMod.editAppealwebMod(data)
  },
}

module.exports = userServ